package com.example.pool;

import jdk.nashorn.internal.codegen.CompilerConstants;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author zhangs
 * @version v1.0
 * @decription 线程池的使用
 * @date 2021/4/7 22:52
 **/
public class PoolDemo {
    public static void main(String[] args) {
        // 创建线程池
        ExecutorService service = new MonitorTP(
                5, 5, 10,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));

        // execute()：用于提交不需要返回值的任务
        service.execute(() -> {
            System.out.println("执行线程！");
        });

        // submit()：用于提交具有返回值的的任务
        Callable<Integer> task = () -> {
            Random random = new Random();
            return random.nextInt(10);
        };
        Future<Integer> future = service.submit(task);
        Integer res = null;
        try {
            res = future.get();
            System.out.println(res);
        } catch (InterruptedException e) {
            // 处理中断异常
        } catch (ExecutionException e) {
            // 处理无法执行异常
            e.printStackTrace();
        } finally {
            // 关闭线程池
            service.shutdown();
        }
    }
}
